import os
import csv
from .audit_system import MedicalRecordAuditSystem


def test_medical_record_audit():
    """
    测试电子病历批量审核系统
    """
    # 准备测试数据
    test_records = [
        {
            "patient_id": "A12345678",
            "diagnosis": "高血压",
            "medications": ["降压药A", "降压药B"],
            "report_date": "2023-05-15"
        },
        {
            "patient_id": "B87654321",
            "diagnosis": "糖尿病",
            "medications": ["胰岛素"],
            "report_date": "2023-05-16"
        },
        {
            "patient_id": "C1234567",  # 错误：ID不足8位数字
            "diagnosis": "感冒",
            "medications": ["感冒药"],
            "report_date": "2023-05-17"
        },
        {
            "patient_id": "D12345678",
            "diagnosis": "",  # 错误：诊断结果为空
            "medications": ["维生素"],
            "report_date": "2023-05-18"
        },
        {
            "patient_id": "E12345678",
            "diagnosis": "胃炎",
            "medications": ["胃药"],
            "report_date": "2023-13-45"  # 错误：日期格式不正确
        }
    ]
    
    # 创建审核系统实例
    audit_system = MedicalRecordAuditSystem(batch_size=2, output_file="test_audit_results.csv")
    
    # 执行审核
    print("开始审核病历记录...")
    audit_system.audit_records(test_records)
    
    # 打印统计信息
    print("\n审核统计信息:")
    audit_system.print_statistics()
    
    # 读取并打印CSV文件内容
    print("\n审核结果详情:")
    try:
        with open("test_audit_results.csv", 'r', encoding='utf-8') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                print(f"{row[0]:<15} {row[1]:<10} {row[2]}")
    except FileNotFoundError:
        print("未找到审核结果文件")
    
    # 清理测试文件
    if os.path.exists("test_audit_results.csv"):
        os.remove("test_audit_results.csv")


if __name__ == "__main__":
    test_medical_record_audit()